package com.zhuhao.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

/**
 * 获取resource下的文件
 *
 * @Author zhuhao
 * @Date 2021/11/29 17:18
 * @desc
 */
public class GetClassPath {

    public static void main(String[] args) throws IOException {
        //这里取得的是resource的绝对路径
        ClassLoader classLoader = GetClassPath.class.getClassLoader();
        URL url = classLoader.getResource("test.properties");
        System.out.println(url.getPath());

        InputStream is = classLoader.getResourceAsStream("test.properties");
        Properties properties = new Properties();
        properties.load(is);

        Set<String> set = properties.stringPropertyNames();
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            System.out.println(key + "\t" + properties.get(key));
        }

        System.out.println("---------------------");

        properties.put("a.b.c.d.e", "你好");
        properties.store(Files.newOutputStream(Paths.get("tmp/test.properties")), "test_demo");

    }
}
